/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.xxl.job.admin.utils;

import com.xxl.job.core.constant.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Http工具类
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class HttpContextUtils {

    public static HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }

        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }

    public static Map<String, String> getParameterMap(HttpServletRequest request) {
        Enumeration<String> parameters = request.getParameterNames();

        Map<String, String> params = new HashMap<>();
        while (parameters.hasMoreElements()) {
            String parameter = parameters.nextElement();
            String value = request.getParameter(parameter);
            if (StringUtils.isNotBlank(value)) {
                params.put(parameter, value);
            }
        }

        return params;
    }


    public static String getLanguage() {
        //默认语言
        String defaultLanguage = "zh-CN";
        //request
        HttpServletRequest request = getHttpServletRequest();
        if (request == null) {
            return defaultLanguage;
        }

        //请求语言
        defaultLanguage = request.getHeader(HttpHeaders.ACCEPT_LANGUAGE);

        return defaultLanguage;
    }

    /**
     * 获取请求的token
     */
    public static String getToken() {
        //request
        HttpServletRequest request = getHttpServletRequest();
        //从header中获取token
        String token = request.getHeader(Constant.TOKEN_HEADER);
        //如果header中不存在token，则从参数中获取token
        if (StringUtils.isBlank(token)) {
            token = request.getParameter(Constant.TOKEN_HEADER);
        }
        return token;
    }

    /**
     * 获得用户id
     */
    public static String getUserId() {
        return getAttribute("userId");
    }

    /**
     * 获得用户名
     */
    public static String getUserName() {
        return getAttribute("userName");
    }

    /**
     * 获得权限id
     */
    public static String getRoleId() {
        return getAttribute("roleId");
    }

    /**
     * 获得权限类型
     */
    public static String getRoleType() {
        return getAttribute("roleType");
    }

    /**
     * 获得分区 id
     */
    public static String getPartitionId() {
        return getAttribute("partitionId");
    }

    /**
     * 获得分区名称
     */
    public static String getPartitionName() {
        return getAttribute("partitionName");
    }

    /**
     * 获得 queueIda
     */
    public static String getQueueId() {
        return getAttribute("queueId");
    }

    public static String getAttribute(String key) {
        HttpServletRequest request = getHttpServletRequest();
        Object value = request.getAttribute(key);
        if (null == value) {
            return "";
        }
        return value.toString();
    }

    public static String getOrigin() {
        HttpServletRequest request = getHttpServletRequest();
        return request.getHeader(HttpHeaders.ORIGIN);
    }


}